package org.groupg.practice;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;

public class HeaderInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
        Request newRequest = originalRequest.newBuilder()
            .header("User-Agent", "MyOkHttpClient/1.0")
            .header("Accept", "application/json")
            // 可以从某个地方动态获取 Token
            .header("Authorization", "Bearer " + getAuthToken())
            .build();
        return chain.proceed(newRequest);
    }

    private String getAuthToken() {
        // 实现你的令牌获取逻辑，例如从安全存储中获取
        return "your_dynamic_token_if_needed";
    }
}

// 在使用 OkHttpClient.Builder 时添加：
// .addInterceptor(new HeaderInterceptor())